//
//  DetailViewController.m
//  11111
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "DetailViewController.h"
#import "sqManager.h"


@interface DetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign)BOOL isBool;
@property (nonatomic, strong) UIBarButtonItem *BarButton;

@property (nonatomic, retain)UIView *detaView; // 分享

@end

@implementation DetailViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addTableView];
    
    self.detailView = [[DetailView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44)];
    [self.tabelView addSubview:self.detailView];
    
    self.detailView.titleLB.text = @"菜名:"; 
    self.detailView.effectLB.text = @"功效:";
    self.detailView.cateLB.text = @"分类:";
    self.detailView.yuanliaoLB.text = @"原料:";
    
    self.detailView.titleTF.text = self.model.title;
    self.detailView.effectTF.text = self.model.effect;
    self.detailView.cateTF.text = self.model.category;
    
    NSString *content = self.model.yuanliao;
    CGFloat height = [self adjustSubviewsWithContent:content];
    
    CGRect newFrame = self.detailView.yuanliaoTF.frame;
    newFrame.size.height = height;
    self.detailView.yuanliaoTF.frame = newFrame;
    
    self.detailView.yuanliaoTF.text = content;
    
    self.detailView.yingyangLB.text = @"营养:";
    NSString *yingyangContent = self.model.yingyang;
    CGFloat yingyangHeight = [self adjustSubviewsWithContent:yingyangContent];
    CGFloat top = self.detailView.yuanliaoTF.bottom + 20;
    CGRect yingyangFrame = self.detailView.yingyangTF.frame;
    yingyangFrame.origin.y = top;
    yingyangFrame.size.height = yingyangHeight;
    self.detailView.yingyangTF.frame = yingyangFrame;
    self.detailView.yingyangTF.text = yingyangContent;
    
    
    NSString *con = self.model.yingyang;
    CGFloat height1 = [self adjustSubviewsWithContent:con];
    CGRect nFrame = self.detailView.yingyangTF.frame;
    nFrame.size.height = height1;
    self.detailView.yingyangTF.frame = nFrame;
    self.detailView.yingyangTF.text = con;
    
    CGFloat top1 = self.detailView.yuanliaoTF.bottom + 20;
    CGRect yingyangFrame1 = self.detailView.yingyangLB.frame;
    yingyangFrame1.origin.y = top1;
    self.detailView.yingyangLB.frame = yingyangFrame1;
    
    [self.detailView.imageView sd_setImageWithURL:[NSURL URLWithString:self.model.thumb_2]];
    // 详情页面的设置图片
    self.BarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"OrangeHeart"] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButton:)];
    
    self.BarButton.image = [[UIImage imageNamed:@"OrangeHeart"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.rightBarButtonItem = self.BarButton;
    
    // 详情收藏弹窗
    self.detaView = [[UIView alloc] initWithFrame:CGRectMake(((kScreenWidth - 200) / 2), 100 * kHeight, 200 * kWidth, 140 * kHeight)];
    self.detaView.backgroundColor = [[UIColor orangeColor]colorWithAlphaComponent:0.6];
    [self.view addSubview:self.detaView];
    
    self.detaView.alpha = 0;

    
    _isBool = NO;
}


- (void)addTableView
{
    self.tabelView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    self.tabelView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tabelView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 700;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    return cell;
}



#pragma mark -- 将self.view1的上面加个分享, 收藏

- (void)rightButton:(UIBarButtonItem *)button

{
   
    [self bgViewApperAnimation];
    self.detaView.alpha = 1;
    // 分享按钮
    UIButton *enjoyButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    enjoyButton.frame = CGRectMake(20 * kWidth, 20 * kHeight, 160 * kWidth, 30 * kHeight);
    [self.detaView addSubview:enjoyButton];
    [enjoyButton setTitle:@"分享" forState:(UIControlStateNormal)];
    [enjoyButton addTarget:self action:@selector(actionEnjoy:) forControlEvents:(UIControlEventTouchUpInside)];
    enjoyButton.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.6];
    // 收藏按钮
    UIButton *shoucangButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    shoucangButton.frame = CGRectMake(20 * kWidth, 55 * kHeight, 160 * kWidth, 30 * kHeight);
    [self.detaView addSubview:shoucangButton];
    [shoucangButton setTitle:@"收藏" forState:(UIControlStateNormal)];
    [shoucangButton addTarget:self action:@selector(shoucangButton:) forControlEvents:(UIControlEventTouchUpInside)];
    shoucangButton.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.6];
//    // 摇一摇按钮
//    UIButton *yaoyiyaoButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
//    yaoyiyaoButton.frame = CGRectMake(20 * kWidth, 90 * kHeight, 160 * kWidth, 30 * kHeight);
//    [self.detaView addSubview:yaoyiyaoButton];
//    [yaoyiyaoButton setTitle:@"摇一摇" forState:UIControlStateNormal];
//    [yaoyiyaoButton addTarget:self action:@selector(yaoyiyaoButton:) forControlEvents:(UIControlEventTouchUpInside)];
//    yaoyiyaoButton.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0.6];
}

// 动画效果
- (void)bgViewApperAnimation
{
    CATransition *animation = [CATransition animation];
    animation.duration = 1.5;
    animation.type = @"rippleEffect";
    animation.subtype = kCATransitionFromTop;
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    [self.detaView.layer addAnimation:animation forKey:@"animation"];
}

// 分享按钮实现方法
- (void)actionEnjoy:(UIButton *)button
{
    //56348f4a67e58e704c0017b2

    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"56348f4a67e58e704c0017b2" shareText:nil shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToQQ, UMShareToLine, UMShareToSina, UMShareToDouban, UMShareToEmail, nil] delegate:nil];
    
    self.detaView.alpha = 0;
    
}

// 收藏按钮
- (void)shoucangButton:(UIBarButtonItem *)button
{
//#pragma  mark -- 插入数据库
//    [[sqManager shareManager] createTable];
//    MenuModel *model = [[MenuModel alloc] init];
//    model.title = self.model.title;
//    model.category = self.model.category;
//    model.thumb = self.model.thumb;
//    model.yingyang = self.model.yingyang;
//    model.yuanliao = self.model.yuanliao;
//    model.effect = self.model.effect;
//
//    [[sqManager  shareManager] insetWith:model];
//    UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
//    [view show];

#pragma  mark -- 插入数据库
   [[sqManager shareManager] createTable];
        MenuModel *model = [[MenuModel alloc] init];
        model.title = self.model.title;
        model.category = self.model.category;
        model.thumb = self.model.thumb;
        model.yingyang = self.model.yingyang;
        model.yuanliao = self.model.yuanliao;
        model.effect = self.model.effect;


        NSMutableArray *array = [[sqManager shareManager] queryAll];
        for (MenuModel *model1 in array) {
            if ([model1.title isEqualToString:self.model.title]) {
                _isBool = YES;                   }

    }

    if (_isBool == YES) {
       
        UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已经收藏过了" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [view show];
         self.detaView.alpha = 0;
       
        return;


    } else {
        [[sqManager  shareManager] insetWith:model];

        UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [view show];
         self.detaView.alpha = 0;
        self.BarButton.image = [[UIImage imageNamed:@"red"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
               return;
    }
    

}



- (CGFloat)adjustSubviewsWithContent:(NSString *)content
{
    //计算活动内容的高度
    CGRect frame = [content boundingRectWithSize:CGSizeMake(self.detailView.yingyangTF.width, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName] context:nil];
    // 更改label高度
    return frame.size.height;
}


@end
